package cn.ly.core.thread;

public class ThreadTest {

	public static class MyThreadLocal extends ThreadLocal {

		@Override
		public void finalize() {
			System.out.println("My threadlocal finalized.");
		}
	}

	public static class My2MB {// 占用内存的大对象
		private byte[] a = new byte[1024 * 1024 * 2];

		@Override
		public void finalize() {
			System.out.println("My2MB finalized.");
		}
	}

	public static void main(String[] args) throws InterruptedException {
		new Thread(new Runnable() {
			@Override
			public void run() {
				ThreadLocal tl = new MyThreadLocal();
				
				tl.set("test1 first set");

				System.out.println("first get:" + tl.get());
				
				tl.set(new My2MB());
				
				System.out.println("second get:" + tl.get());

				tl = null;// 断开ThreadLocal的强引用
				System.out.println("Full GC 1");
				System.gc();

			}
		}).start();
		
		
		
		System.out.println("Full GC 2");
		System.gc();
		Thread.sleep(1000);
		System.out.println("Full GC 3");
		System.gc();
		Thread.sleep(1000);
		System.out.println("Full GC 4");
		System.gc();
		Thread.sleep(1000);
	}
}
